#爬取并下载百度贴吧 美腿吧第一页图片
import requests
from urllib.request import urlretrieve
from bs4 import BeautifulSoup

def get_link(): #获取首页所有有效帖子链接
	url = "https://tieba.baidu.com/f?ie=utf-8&kw=华北科技学院" #可以更改关键字下载其他贴吧
	r = requests.get(url)
	html = r.text
	soup = BeautifulSoup(html,'html.parser')
	links = set()
	link = soup.findAll('a',{'class':'j_th_tit'})
	for url in link:
		links.add(url['href'])
	return links
def download(links):
	count = 0
	for link in links:
		url = "https://tieba.baidu.com"+link
		r = requests.get(url)
		html = r.text
		soup = BeautifulSoup(html,'html.parser')
		url = soup.findAll('img',{'class':'BDE_Image'})
		for i in url:
			count += 1
			urlretrieve(i['src'],str(count)+".jpg" )
			print("成功下载")
download(get_link())



